package org.makcro.sms.services.chat.web.client.widgets.main;

import java.util.ArrayList;

import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.client.widgets.NewMessageTextRendererImpl;
import org.makcro.sms.services.chat.web.shared.dto.QuestionDto;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.DateTimeFormat;

public class NewMessagesPanel extends ContentPanel {

	private Button					btnAnswerMessage;
	private Button					btnRefresh;
	private Grid<QuestionDto>		grid;
	private ListStore<QuestionDto>	store;
	private DateTimeFormat			dateTimeFormat	= DateTimeFormat.getFormat( "dd/MM/yyyy 'u' hh:mm" );

	public NewMessagesPanel() {

		setHeading( "Prispele poruke" );
		FitLayout layout = new FitLayout();

		setLayout( layout );
		setScrollMode( Scroll.AUTOY );

		getButtonBar().add( getBtnAnswerMessage() );
		getButtonBar().add( getBtnRefresh() );

		add( getGrid() );
	}

	protected void onGridDoubleClick(GridEvent<QuestionDto> arg0) {
		// check if any row was selected
		QuestionDto selectedItem = grid.getSelectionModel().getSelectedItem();

		if (selectedItem != null) {
			QuestionDto selectedQuestion = arg0.getModel();
			AppEvent gridDoubleClickEvent = new AppEvent( AppEvents.PREPARE_ANSWER, selectedQuestion );
			Dispatcher.get().dispatch( gridDoubleClickEvent );
		}
	}

	private ColumnModel getColumnModel() {
		ArrayList<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add( new ColumnConfig( "fromNumber", "Posiljalac", 150 ) );
		
		ColumnConfig dateColumn = new ColumnConfig( "dateReceived", "Vreme prijema", 150 );
		dateColumn.setDateTimeFormat( dateTimeFormat );
		columns.add( dateColumn );
		
		ColumnConfig messageTextColumn = new ColumnConfig( "question", "Tekst poruke", 180 );
		messageTextColumn.setRenderer(new NewMessageTextRendererImpl());

			
		columns.add( messageTextColumn );

		return new ColumnModel( columns );
	}

	protected void onQuestionSelected(SelectionChangedEvent<QuestionDto> arg0) {

	}

	private Button getBtnAnswerMessage() {
		if (btnAnswerMessage == null) {
			btnAnswerMessage = new Button( "Odgovori" );
			btnAnswerMessage.addListener( Events.Select, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent be) {
					onBtnAnswerSelected();
				}

			} );
		}
		return btnAnswerMessage;
	}

	protected void onBtnAnswerSelected() {
		// check if any question is selected
		QuestionDto selectedItem = grid.getSelectionModel().getSelectedItem();
		if (selectedItem != null) {
			AppEvent appEvent = new AppEvent( AppEvents.PREPARE_ANSWER, selectedItem );
			Dispatcher.get().dispatch( appEvent );
		}
		else {
			Info.display( "Obavestenje", "Morate izabrati poruku" );
		}

	}

	public Button getBtnRefresh() {
		if (btnRefresh == null) {
			btnRefresh = new Button();
			btnRefresh.setText( "Osvezi" );
			btnRefresh.addListener( Events.Select, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent be) {
					onRefreshButtonClick( be );

				}
			} );
		}
		return btnRefresh;
	}

	protected void onRefreshButtonClick(ButtonEvent be) {
		Dispatcher.get().dispatch( AppEvents.NEW_MESSAGES_UPDATE );
	}

	public Grid<QuestionDto> getGrid() {
		if (this.grid == null) {
			this.grid = new Grid<QuestionDto>( getStore(), getColumnModel() );
			this.grid.setBorders( false );
			this.grid.setAutoHeight( false );
			this.grid.setAutoExpandColumn( "question" );

			this.grid.addListener( Events.OnDoubleClick, new Listener<GridEvent<QuestionDto>>() {

				@Override
				public void handleEvent(GridEvent<QuestionDto> arg0) {
					onGridDoubleClick( arg0 );
				}
			} );
		}
		return grid;
	}

	public ListStore<QuestionDto> getStore() {
		if (this.store == null) {
			this.store = new ListStore<QuestionDto>();
			this.store.setMonitorChanges( true );
		}
		return store;
	}

	public void setStore(ListStore<QuestionDto> store) {
		this.store = store;
	}
}
